<?php

namespace App\Http\Controllers\Foundation;

use App\Model\Foundation\Teacher;
use Illuminate\Support\Facades\DB;
use App\Model\Business\Schedule;
use App\Model\Business\ScheduleStuden;
use App\Model\Foundation\Statistic;
use App\Model\Foundation\Student;
use Illuminate\Http\Request;
use App\Model\System\Token;
use Laravel\Lumen\Routing\Controller as BaseController;


use App\Model\Foundation\CourseType;
use App\Http\Controllers\MessagePacket;


class StatisticController extends BaseController
{
    use MessagePacket;


    function  __construct(){}

    /**
     * 获取教师课时统计
     * @param Request $request
     * @return array
     */
    function  getTeacherSchedule(Request $request)
    {
        $endTime = '';
        $beginTime = '';
        if($request->has('endtime')){
            $endTime = $request->get('endtime');
        }
        if($request->has('begintime')){
            $beginTime = $request->get('begintime');
        }
        if($endTime){
            $endTimeStemp = strtotime($endTime);
            $endTimeStemp = $endTimeStemp+(60*60*24);
            $endTime = date('Y-m-d H:i:s',$endTimeStemp);
        }

        $infos = Statistic::teacherSchedules($beginTime, $endTime);
        $result = [];
        foreach ($infos as $info){
            $course = [];
            foreach ($info['coursetypes'] as $k=>$v){
                $kv =[];
                $kv['id'] = $k;
		$v = round($v,1);
                $kv['counts'] =$v;
                $course[] = $kv;
            }
            $info['coursetypes'] = $course;
            $result[] = $info;
        }

        return $this->success($result);

    }

    /**
     * 获取学生课时统计
     * @param Request $request
     * @return array
     */
    function getStudentSchedule(Request $request)
    {
        $endTime = '';
        $beginTime = '';
        if($request->has('endtime')){
            $endTime = $request->get('endtime');

        }else{
            $endTime = date('Y-m-d H:i:s', time()+86400*30);
        }
        if($request->has('begintime')){
            $beginTime = $request->get('begintime');
        }else{
            $beginTime = date('Y-m-d H:i:s', time()-86400*30);
        }
        # 条件修正
        if($endTime){
            $endTimeStemp = strtotime($endTime);
            $endTimeStemp = $endTimeStemp+(60*60*24);
            $endTime = date('Y-m-d H:i:s',$endTimeStemp);
        }
        $infos = Statistic::studentSchedules($beginTime, $endTime);

        $result = [];
        foreach ($infos as $info){
            $course = [];
            foreach ($info['coursetypes'] as $k=>$v){
                $kv =[];
                $kv['id'] = $k;
		$v = round($v,1);
                $kv['counts'] =$v;
                $course[] = $kv;
            }
            $info['coursetypes'] = $course;
            $result[] = $info;
        }

        return $this->success($result);
    }


    /**
     * 获取学生课时明细列表
     * @param Request $request
     * @return array
     */
    function getStudentScheduleList(Request $request, $id)
    {
        $stid = $id;
        $endtime = '';
        $begintime = '';

        if($request->has('studentid')){
            $stid = $request->get('studentid');
        }
        if($request->has('endtime')){
            $endtime = $request->get('endtime');
        }
        if($request->has('begintime')){
            $begintime = $request->get('begintime');
        }
        # 条件修正
        if($endtime){
            $endTimeStemp = strtotime($endtime);
            $endTimeStemp = $endTimeStemp+(60*60*24);
            $endtime = date('Y-m-d H:i:s',$endTimeStemp);
        }

        $results = ScheduleStuden::getStudentSchedules($stid, $begintime, $endtime);

        return $this->success($results);
    }

    /**
     * 获取老师课时明细列表
     * @param Request $request
     * @return array
     */
    function getTeacherScheduleList(Request $request, $id)
    {
        $query = DB::table('schedules')
            ->where('schedules.status',1)
            ->where('schedules.teacheruid',$id)
            ->select('schedules.teacheruid','schedules.name', 'schedules.coursetypename', 'schedules.begintime',
             'schedules.endtime','schedules.scheduleid','teachers.ClassInNo',
                'eeosync.firstEnterTime','eeosync.lastExitTime','eeosync.studyTime')
            ->join('teachers', 'teachers.teacheruid', '=', 'schedules.teacheruid')
            ->leftJoin('eeosync', function ($join){
                $join->on('eeosync.scheduleid', '=', 'schedules.scheduleid')
                    ->on('eeosync.userid', '=', 'teachers.ClassInNo');
            });
        # 按老师查询
        if($request->has('teacheruid')) {
            $tuid = intval($request->get('teacheruid'));
            $query->where('teacheruid', $tuid);
        }
        # 指定开始时间
        if($request->has('begintime')){
            $query->where('begintime', '>=', $request->get('begintime'));
        }
        # 指定结束时间
        if($request->has('endtime')){
            $endtime = $request->get('endtime');
            # 条件修正
            if($endtime){
                $endTimeStemp = strtotime($endtime);
                $endTimeStemp = $endTimeStemp+(60*60*24);
                $endtime = date('Y-m-d H:i:s',$endTimeStemp);
            }
            $query->where('endtime', '<', $endtime);
        }
        $schedules = $query->get();

        #附加教师名字
        foreach ($schedules as $k=>$item){
            $teacher = Teacher::find($item->teacheruid);
            if($teacher){
                $schedules[$k]->teacherName = $teacher->lastname;
            }
            $schedules[$k]->scheduletimes = Schedule::calculRealSchedules($item->begintime, $item->endtime);
        }
        # 附加学生
        foreach ($schedules as $k=>$item){
            $scheduleStudent = ScheduleStuden::getStudentByScheduleID($item->scheduleid);
            $begin = date('H:i', strtotime($item->begintime));
            $end = date('H:i', strtotime($item->endtime));
            $schedules[$k]->period = "$begin - $end";
            $students = [];
            $auditors = [];
            foreach ($scheduleStudent as $student){
                if($student->identity==1){
                    $students[] = $student;
                }else{
                    $auditors[] = $student;
                }
            }
            $schedules[$k]->students = $students ;
            $schedules[$k]->auditors = $auditors ;
        }

        return $this->success($schedules);
    }
}
